package com.cxrj.zhsq.userManage.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cxrj.zhsq.annotation.SystemControllerLog;
import com.cxrj.zhsq.userManage.entity.Organization;
import com.cxrj.zhsq.userManage.service.OrganizationService;

/**
 * @Author: WangBo
 * @Description:
 * @Date: Create in 15:30 2017/11/29
 * @Modified By:
 */
@RestController
public class OrganizationController {

	@Autowired
	private OrganizationService organizationService;

	/**
	 * @Author: WangBo
	 * @Description: 查询组织树
	 * @param
	 * @Date: 10:55 2017/11/30
	 */
	@RequestMapping("selectOrganizationTree")
	public List<Map<String, Object>> selectOrganizationTree() {
		List<Map<String, Object>> list = organizationService.selectOrganizationTree(new HashMap<String, Object>());
		if (list == null) {
			list = new ArrayList<Map<String, Object>>();
		}
		return list;
	}

	/**
	 * @Author: WangBo
	 * @Description: 查询组织树人数
	 * @param
	 * @Date: 10:55 2017/11/30
	 */
	@RequestMapping("selectOrganizationTreeNum")
	public List<Map<String, Object>> selectOrganizationTreeNum() {
		return organizationService.selectOrganizationTreeNum(new HashMap<String, Object>());
	}

	/**
	 * @Author: WangBo
	 * @Description: 验证code是否重复
	 * @param map
	 * @Date: 15:52 2017/11/30
	 */
	@RequestMapping(value = "checkOrganizationCode", method = RequestMethod.POST)
	public Map<String, Object> checkOrganizationCode(@RequestParam Map<String, Object> map) {
		return organizationService.selectOrganizationCode(map);
	}

	/**
	 * @Author: WangBo
	 * @Description: 新增组织
	 * @param map
	 *            参数map
	 * @Date: 15:55 2017/11/30
	 */
	@RequestMapping(value = "insertOrganization", method = RequestMethod.POST)
	@SystemControllerLog(description = "新增部门")
	public Map<String, Object> insertOrganization(Organization organization, HttpSession session) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			organization.setEditAccount(session.getAttribute("userAccount").toString());
			organizationService.insertOrganization(organization);
			resultMap.put("result", 0);
		} catch (Exception e) {
			resultMap.put("result", -1);
		}
		return resultMap;
	}

	/**
	 * @Author: WangBo
	 * @Description: 修改组织
	 * @param map
	 *            参数map
	 * @Date: 15:55 2017/11/30
	 */
	@RequestMapping(value = "updateOrganization", method = RequestMethod.POST)
	@SystemControllerLog(description = "修改部门")
	public Map<String, Object> updateOrganization(@RequestParam Map<String, Object> map, HttpSession session) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			map.put("editAccount", session.getAttribute("userAccount"));
			organizationService.updateOrganization(map);
			resultMap.put("result", 0);
		} catch (Exception e) {
			resultMap.put("result", -1);
		}
		return resultMap;
	}

	/**
	 * @Author: WangBo
	 * @Description: 删除组织
	 * @param map
	 *            参数map
	 * @Date: 15:55 2017/11/30
	 */
	@RequestMapping(value = "deleteOrganization", method = RequestMethod.POST)
	@SystemControllerLog(description = "删除部门")
	public Map<String, Object> deleteOrganization(@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			organizationService.deleteOrganization(map);
			resultMap.put("result", 0);
		} catch (Exception e) {
			resultMap.put("result", -1);
		}
		return resultMap;
	}

	@RequestMapping("selectTopOrganization")
	public Map<String, Object> selectTopOrganization() {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			resultMap.put("organization", organizationService.selectTopOrganization());
			resultMap.put("result", 0);
		} catch (Exception e) {
			resultMap.put("result", -1);
		}
		return resultMap;
	}
	/** 分页查询社区
     * @return
     */
    @RequestMapping(value="selectCommunityList",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> selectCommunityList(HttpServletRequest request, HttpServletResponse response){
        return organizationService.selectCommunityList(request,response);
    }
   /**
    * 
    * @author Hanxing
    * @date 2018年3月1日 上午11:27:59
    * @Description 查询所有社区列表
    * key ：  list
    */
    @RequestMapping(value="selectAllCommunityList",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> selectAllCommunityList(){
        return organizationService.selectAllCommunityList();
    }
}
